import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import plotly.express as px
import pymc3
import plotly.graph_objects as go
import plotly.figure_factory as ff
size=20000
#facebook
facebook_budget=15*4
conversion=[21,57]
reach=[554,1600]
#ecommerce
ali_price=12.35
shipping=0
selling_price=(ali_price+shipping)+(ali_price+shipping)*1.4
#payment_gate
two_checkout=(selling_price+shipping)*0.035+0.35
stripe=(selling_price+shipping)*0.029+0.3
#arrays
prior_arr=np.empty(size)
likelihood_arr=np.empty(size)
ecom_revenu_arr=np.empty(size)
ali_cost_arr=np.empty(size)
two_checkout_cost_arr=np.empty(size)
stripe_cost_arr=np.empty(size)
profit_two_arr=np.empty(size)
profit_stripe_arr=np.empty(size)
for i in range(size):
prior=np.random.beta(np.random.randint(conversion[0],conversion[1]),(np.random.randint(reach[0],reach[1])-np.random.randint(conversion[0],conversion[1])))
likelihood=np.random.binomial(np.mean(reach),prior)
ecom_revenu=selling_price*likelihood
ali_cost=(ali_price+shipping)*likelihood
two_checkout_cost=two_checkout*likelihood
stripe_cost=stripe*likelihood
profit_two=ecom_revenu-ali_cost-two_checkout_cost-facebook_budget
profit_stripe=ecom_revenu-ali_cost-stripe_cost-facebook_budget
prior_arr[i]=round(prior,3)
likelihood_arr[i]=likelihood
ecom_revenu_arr[i]=round(ecom_revenu,3)
ali_cost_arr[i]=round(ali_cost,3)
two_checkout_cost_arr[i]=round(two_checkout_cost,3)
stripe_cost_arr[i]=round(stripe_cost,3)
profit_two_arr[i]=round(profit_two,3)
profit_stripe_arr[i]=round(profit_stripe,3)
df=pd.DataFrame(data={"prior_arr":prior_arr,"likelihood_arr":likelihood_arr,"ecom_revenu_arr":ecom_revenu_arr,
"ali_cost_arr":ali_cost_arr,"two_checkout_cost_arr":two_checkout_cost_arr,"stripe_cost_arr":stripe_cost_arr,
"profit_two_arr":profit_two_arr,"profit_stripe_arr":profit_stripe_arr})
df["profit_per_unit"]=round(df["profit_two_arr"]/df["likelihood_arr"],3)
print("Selling price is:",selling_price)
for i in ["profit_two_arr","profit_per_unit","prior_arr","likelihood_arr","ali_cost_arr"]:
fig1=px.histogram(df,x=i,marginal='box')
fig1.add_trace(
go.Scatter(
x=[pymc3.stats.hpd(df[i])[0], pymc3.stats.hpd(df[i])[0]],
y=[0, 200],
mode="lines",
line=go.scatter.Line(color="red"),
showlegend=False))
fig1.add_trace(
go.Scatter(
x=[pymc3.stats.hpd(df[i])[1], pymc3.stats.hpd(df[i])[1]],
y=[0, 200],
mode="lines",
line=go.scatter.Line(color="red"),
showlegend=False))
if i =="prior_arr":
print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df[i])[0]*100,2),"%","and",round(pymc3.stats.hpd(df[i])[1]*100,2),"%")
else:
print("The HPD of the "+i+" is bettwen",pymc3.stats.hpd(df[i])[0],"and",pymc3.stats.hpd(df[i])[1])
fig1.show()
print("________________________________________________")
#worst case
size=20000
#facebook
facebook_budget=15*4
conversion=[21]
reach=[1600]
#ecommerce
ali_price=12.35
shipping=0
selling_price=(ali_price+shipping)+(ali_price+shipping)*1.4
#payment_gate
two_checkout=(selling_price+shipping)*0.035+0.35
stripe=(selling_price+shipping)*0.029+0.3
#arrays
prior_arr=np.empty(size)
likelihood_arr=np.empty(size)
ecom_revenu_arr=np.empty(size)
ali_cost_arr=np.empty(size)
two_checkout_cost_arr=np.empty(size)
stripe_cost_arr=np.empty(size)
profit_two_arr=np.empty(size)
profit_stripe_arr=np.empty(size)
for i in range(size):
prior=np.random.beta(np.mean(conversion),(np.mean(reach)-np.mean(conversion)))
likelihood=np.random.binomial(np.mean(reach),prior)
ecom_revenu=selling_price*likelihood
ali_cost=(ali_price+shipping)*likelihood
two_checkout_cost=two_checkout*likelihood
stripe_cost=stripe*likelihood
profit_two=ecom_revenu-ali_cost-two_checkout_cost-facebook_budget
profit_stripe=ecom_revenu-ali_cost-stripe_cost-facebook_budget
prior_arr[i]=round(prior,3)
likelihood_arr[i]=likelihood
ecom_revenu_arr[i]=round(ecom_revenu,3)
ali_cost_arr[i]=round(ali_cost,3)
two_checkout_cost_arr[i]=round(two_checkout_cost,3)
stripe_cost_arr[i]=round(stripe_cost,3)
profit_two_arr[i]=round(profit_two,3)
profit_stripe_arr[i]=round(profit_stripe,3)
df=pd.DataFrame(data={"prior_arr":prior_arr,"likelihood_arr":likelihood_arr,"ecom_revenu_arr":ecom_revenu_arr,
"ali_cost_arr":ali_cost_arr,"two_checkout_cost_arr":two_checkout_cost_arr,"stripe_cost_arr":stripe_cost_arr,
"profit_two_arr":profit_two_arr,"profit_stripe_arr":profit_stripe_arr})
df["profit_per_unit"]=round(df["profit_two_arr"]/df["likelihood_arr"],3)
print("Selling price is:",selling_price)
for i in ["profit_two_arr","profit_per_unit","prior_arr","likelihood_arr","ali_cost_arr"]:
fig1=px.histogram(df,x=i,marginal='box')
fig1.add_trace(
go.Scatter(
x=[pymc3.stats.hpd(df[i])[0], pymc3.stats.hpd(df[i])[0]],
y=[0, 200],
mode="lines",
line=go.scatter.Line(color="red"),
showlegend=False))
fig1.add_trace(
go.Scatter(
x=[pymc3.stats.hpd(df[i])[1], pymc3.stats.hpd(df[i])[1]],
y=[0, 200],
mode="lines",
line=go.scatter.Line(color="red"),
showlegend=False))
if i =="prior_arr":
print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df[i])[0]*100,2),"%","and",round(pymc3.stats.hpd(df[i])[1]*100,2),"%")
else:
print("The HPD of the "+i+" is bettwen",pymc3.stats.hpd(df[i])[0],"and",pymc3.stats.hpd(df[i])[1])
fig1.show()
print("________________________________________________")
#worst case
size=10000
#facebook
facebook_budget=15*4
conversion=[21]
reach=[1600]
#ecommerce
ali_price=12.35
shipping=0
selling_price=(ali_price+shipping)+(ali_price+shipping)*0.5
#payment_gate
two_checkout=(selling_price+shipping)*0.035+0.35
stripe=(selling_price+shipping)*0.029+0.3
#arrays
prior_arr=np.empty(size)
likelihood_arr=np.empty(size)
ecom_revenu_arr=np.empty(size)
ali_cost_arr=np.empty(size)
two_checkout_cost_arr=np.empty(size)
stripe_cost_arr=np.empty(size)
profit_two_arr=np.empty(size)
profit_stripe_arr=np.empty(size)
for i in range(size):
prior=np.random.beta(np.mean(conversion),(np.mean(reach)-np.mean(conversion)))
likelihood=np.random.binomial(np.mean(reach),prior)
ecom_revenu=selling_price*likelihood
ali_cost=(ali_price+shipping)*likelihood
two_checkout_cost=two_checkout*likelihood
stripe_cost=stripe*likelihood
profit_two=ecom_revenu-ali_cost-two_checkout_cost-facebook_budget
profit_stripe=ecom_revenu-ali_cost-stripe_cost-facebook_budget
prior_arr[i]=round(prior,3)
likelihood_arr[i]=likelihood
ecom_revenu_arr[i]=round(ecom_revenu,3)
ali_cost_arr[i]=round(ali_cost,3)
two_checkout_cost_arr[i]=round(two_checkout_cost,3)
stripe_cost_arr[i]=round(stripe_cost,3)
profit_two_arr[i]=round(profit_two,3)
profit_stripe_arr[i]=round(profit_stripe,3)
df=pd.DataFrame(data={"prior_arr":prior_arr,"likelihood_arr":likelihood_arr,"ecom_revenu_arr":ecom_revenu_arr,
"ali_cost_arr":ali_cost_arr,"two_checkout_cost_arr":two_checkout_cost_arr,"stripe_cost_arr":stripe_cost_arr,
"profit_two_arr":profit_two_arr,"profit_stripe_arr":profit_stripe_arr})
df["profit_per_unit"]=round(df["profit_two_arr"]/df["likelihood_arr"],3)
print("Selling price is:",selling_price)
for i in ["profit_two_arr","profit_per_unit","prior_arr","likelihood_arr","ali_cost_arr"]:
fig1=px.histogram(df,x=i,marginal='box',)
fig1.add_trace(
go.Scatter(
x=[pymc3.stats.hpd(df[i])[0], pymc3.stats.hpd(df[i])[0]],
y=[0, 200],
mode="lines",
line=go.scatter.Line(color="red"),
showlegend=False))
fig1.add_trace(
go.Scatter(
x=[pymc3.stats.hpd(df[i])[1], pymc3.stats.hpd(df[i])[1]],
y=[0, 200],
mode="lines",
line=go.scatter.Line(color="red"),
showlegend=False))
if i =="prior_arr":
print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df[i])[0]*100,2),"%","and",round(pymc3.stats.hpd(df[i])[1]*100,2),"%")
else:
print("The HPD of the "+i+" is bettwen",pymc3.stats.hpd(df[i])[0],"and",pymc3.stats.hpd(df[i])[1])
fig1.show()
print("________________________________________________")
for i in ["profit_two_arr","profit_per_unit","prior_arr","likelihood_arr","ali_cost_arr"]:
fig1=px.histogram(df,x=i,marginal='box',histnorm="probability")
fig1.add_trace(
go.Scatter(
x=[pymc3.stats.hpd(df[i])[0], pymc3.stats.hpd(df[i])[0]],
y=[0, 0.1],
mode="lines",
line=go.scatter.Line(color="red"),
showlegend=False))
fig1.add_trace(
go.Scatter(
x=[pymc3.stats.hpd(df[i])[1], pymc3.stats.hpd(df[i])[1]],
y=[0, 0.1],
mode="lines",
line=go.scatter.Line(color="red"),
showlegend=False))
if i =="prior_arr":
print("The HPD of the "+i+" is bettwen",round(pymc3.stats.hpd(df[i])[0]*100,2),"%","and",round(pymc3.stats.hpd(df[i])[1]*100,2),"%")
else:
print("The HPD of the "+i+" is bettwen",pymc3.stats.hpd(df[i])[0],"and",pymc3.stats.hpd(df[i])[1])
fig1.show()
print("________________________________________________")